﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NLBS.Utility;
using System.IO;
using System.Text;
using NLBS.Control;

namespace NLBS.Admin.control
{
    public partial class attachments : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            checkAdmin();

            string localPath = Request.PhysicalApplicationPath + BlogInfo.UploadPath;
            string path = string.Empty;
            string localTransPath = Path.GetFullPath(localPath);
            StringBuilder sb = new StringBuilder();
            
            if (Request.QueryString["file"] != null)
            {
                FileHelp.DeleteFile(localPath + HttpUtility.UrlDecode(Request.QueryString["file"]));
                FileInfo deaeteFile=new FileInfo(localPath + HttpUtility.UrlDecode(Request.QueryString["file"]));
                
                if (localTransPath != deaeteFile.Directory.FullName+"\\")
                {
                    localPath = deaeteFile.Directory.FullName;
                    sb.AppendFormat("<a href=\"#\" onclick=\"AdminFile.GetFile('{0}')\">", GetParentPath(localPath));
                    sb.Append("<img src=\"/styles/default/images/icon_folder_up.gif\" /> 父目录..</a><br /><br />");
                    
                }
            }
            if (Request.QueryString["folder"] != null)
            {
                FileHelp.DeleteDir(localPath + HttpUtility.UrlDecode(Request.QueryString["folder"]));
                string parentDir = Directory.GetParent(localPath + HttpUtility.UrlDecode(Request.QueryString["folder"])).FullName;
                if (localTransPath != parentDir + "\\")
                {
                    localPath = parentDir;
                    sb.AppendFormat("<a href=\"#\" onclick=\"AdminFile.GetFile('{0}')\">", GetParentPath(localPath));
                    sb.Append("<img src=\"/styles/default/images/icon_folder_up.gif\" /> 父目录..</a><br /><br />");
                }
            }

            if (String.IsNullOrEmpty(Request.QueryString["path"]))
            {
                path = localPath;
            }
            else
            {
                path = localPath + HttpUtility.UrlDecode(Request.QueryString["path"]);
                
                sb.AppendFormat("<a href=\"#\" onclick=\"AdminFile.GetFile('{0}')\">", GetParentPath(path));
                sb.Append("<img src=\"/styles/default/images/icon_folder_up.gif\" /> 父目录..</a><br /><br />");
            }
         
            DirectoryInfo[] dirs = FileHelp.GetDirectory(path);
            FileInfo[] files = FileHelp.GetFiles(path);
            
            foreach (DirectoryInfo dir in dirs)
            {
                //long fileLength = 0;
                string realName = dir.FullName.Replace(Request.PhysicalApplicationPath + BlogInfo.UploadPath.Replace("\\", "").Replace("/", ""), "").Replace("\\", "/");
                sb.AppendFormat("<a href=\"#\" onclick=\"AdminFile.GetFile('{0}')\">",realName);
                sb.Append("<img src=\"/styles/default/images/icon_folder.gif\" />");
                sb.AppendFormat("<b>{0}</b></a> |", dir.Name);
                sb.AppendFormat("<a href=\"#\" onclick=\"AdminFile.DeleteFolder('{0}')\">删除</a>", realName);
                sb.AppendFormat("{0} Kb |",getDirLength(dir)/ 1024);
                sb.Append("<br />");
            }
            foreach (FileInfo item in files)
            {
                sb.AppendFormat("<a href=\"{0}\" target=\"_blank\">",Request.ApplicationPath+item.FullName.Replace(Request.PhysicalApplicationPath, ""));
                sb.Append("<img src=\"/styles/default/images/icon_file.gif\" />");
                sb.AppendFormat("<b>{0}</b></a> |", item.FullName.Replace(item.DirectoryName + "\\", ""));
                sb.AppendFormat("{0} |", item.Length);
                sb.AppendFormat("<a href=\"#{0}\" onclick=\"AdminFile.DeleteFile('{0}')\">删除</a>", item.FullName.Replace(Request.PhysicalApplicationPath +BlogInfo.UploadPath.Replace("\\","").Replace("/",""), "").Replace("\\", "/"));
                sb.Append("<br />");

            }

            sb.Append("<br />");
            sb.AppendFormat("<span class=\"comment-text\">合计: {0}&nbsp;条目 (您只能删除空文件夹)</span>", dirs.Length + files.Length);
            Label1.Text = sb.ToString();
        }

        private long getDirLength(DirectoryInfo dir)
        {
            long size = 0;
            DirectoryInfo[] chindDir = dir.GetDirectories();
            FileInfo[] chindFile = dir.GetFiles();
            if ((chindDir.Length + chindFile.Length) == 0)
            {
                return 0;
            }
            else
            {
                if (chindFile.Length > 0)
                {
                    foreach (FileInfo item in dir.GetFiles())
                    {
                        size += item.Length;
                    }
                }
                if (chindDir.Length > 0)
                {
                    foreach (DirectoryInfo itemDir in chindDir)
                    {
                        size += getDirLength(itemDir);
                    }
                }
            }
            return size;
        }

        private string GetParentPath(string path)
        {
            return Directory.GetParent(path).FullName.Replace(Request.PhysicalApplicationPath + BlogInfo.UploadPath.Replace("\\", "").Replace("/", ""), "").Replace("\\", "/");
        }

        private void RenderParentPath()
        {

        }
    }
}